const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
module.exports = {
    async getArtList(req, res) {
        //获取url值
        const { current, size, user_id } = req.query;
        console.log("-------", req.query);
        let items;
        if (user_id) { //小程序
            knex('user_cloud_tab').select(
                'user_cloud_tab.id',
                "user_id",
                'title',
                'commentNum',
                'isshaw_comment',
                'isshaw_friends',
                'imgList',
                'updata_time',
                'create_time',
                'username',
                'nickName',
                "avatarUrl",
                "like_user_ids",
                "like_friend_ids",
                "like_community_ids",
            ).
            leftJoin(
                'user_tab',
                'user_cloud_tab.user_id', 'user_tab.id'
            ).where({
                user_id: user_id
            }).orderBy(
                'id', 'desc'
            ).then(resp => {
                // console.log('resp----', resp)
                if (resp.length > 0) {
                    console.log("resp3333--")
                    items = {
                        code: 200,
                        data: resp,
                        msg: "获取列表成功",
                        dataCount: resp.length,
                        success: true
                    }
                    res.send(items)
                } else {
                    res.send({
                        data: [],
                        msg: "获取失败",
                        success: false
                    });
                }
            })

        } else { //后台
            knex('user_cloud_tab').select().then(resp => {
                //密码加密
                data = resp;
            }).then(() => {
                if (data.length > 0) {
                    items = {
                        code: 200,
                        data: {
                            current: current,
                            hitCount: false,
                            orders: [],
                            pages: 1,
                            records: data,
                            searchCount: true,
                            size: size,
                            total: 30
                        },
                        msg: "操作成功",
                        success: true
                    }
                    res.send(items)
                } else {
                    res.send({
                        data: [],
                        msg: "操作失败",
                        success: false
                    });
                }
            })
        }
    },

    //新增
    async postNewArt(req, res) {
        let { user_id, title, imgList, create_time } = req.body;
        console.log("---req.body--", req.body)
        if (imgList === []) {
            imgList = null
        }
        knex('user_cloud_tab').insert({
                user_id: user_id,
                title: title,
                imgList: imgList,
                isshaw_friends: 0,
                isshaw_comment: 0,
                likeNum: 0,
                commentNum: 0,
                create_time: create_time,
            }).then(resp => {
                console.log("res", resp);
                if (resp) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
            //res.send('postUpdate');
    },
    //发布
    async postPublish(req, res) {
        let { cloud_id, user_id, title, imgList, create_time } = req.body;
        console.log("---req.body--", req.body)
        if (imgList.length === 0) {
            imgList = null;
        }
        //判断是否有此条数据
        knex("comment_tab").select().where({ id: cloud_id }).then((resId) => {
            if (resId.length === 0) {
                //修改cloud表中发布
                knex('user_cloud_tab').update({
                    isshaw_comment: 1,
                }).where({
                    id: cloud_id
                }).then((resup) => {
                    if (!resup) {
                        res.send({
                            code: 4001,
                            msg: "操作失败",
                            success: true
                        })
                    }
                })
                knex('community_check_tab').insert({ //审核表新增
                    cloud_id: cloud_id,
                    user_id: user_id,
                    title: title,
                    imgList: imgList,
                    comment_id: cloud_id,
                    create_time: create_time,
                }).then(resp => {
                    console.log("res", resp);
                    if (resp) {
                        res.send({
                            code: 200,
                            msg: "操作成功！",
                            success: true
                        })
                    } else {
                        res.send({
                            code: 4001,
                            msg: "操作失败",
                            success: true
                        })
                    }
                })
            } else {
                console.log("已经存在")
                res.send({
                    code: 4002,
                    msg: "已经存在",
                    success: true
                })
            }
        })
    },
    async postUpdateArt(req, res) {
        let { id, title, isshaw_comment, isshaw_friends, imgList, updata_time } = req.body;
        if (imgList === []) {
            imgList = null
        }
        console.log("---------------", req.body)
            //后台
        if (isshaw_comment) {
            knex('user_cloud_tab').update({
                title: title,
                isshaw_comment: isshaw_comment,
                isshaw_friends: isshaw_friends,
                imgList: imgList,
                updata_time: updata_time,
            }).where({ id: id }).then(resp => {
                console.log(resp);
                if (resp === 1) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
        } else { //用户
            knex('user_cloud_tab').update({
                title: title,
                imgList: imgList,
                updata_time: updata_time,
            }).where({ id: id }).then(resp => {
                console.log(resp);
                if (resp === 1) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
        }

    },

    async postRemoveArt(req, res) {
        console.log("res._parsedUrl", req.query.id);
        const id = req.query.id;

        knex('user_cloud_tab').delete().where({ id: id }).then(resp => {
            console.log(resp);
            if (resp) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
}